#include "playscene.h"
#include<QDebug>
#include<QMenuBar>
#include<QTimer>
#include<QPainter>
#include"mypushbutton.h"
#include<QLabel>
#include"mycoin.h"
#include<dataconfig.h>
#include<QSize>
#include<QPropertyAnimation>
//playScene::playScene(QWidget *parent)
//    : QMainWindow{parent}
//{

//}

playScene::playScene(int level)
{
    QString str = QString("进入了第%1关").arg(level);
    qDebug()<<str;
    this->levelIndex=level;

    //场景布局
    //窗口的简单设置（大小，图标，名称）
    setFixedSize(320,588);
    setWindowIcon(QIcon(":/res/Coin0001.png"));
    setWindowTitle("关卡界面");

    //创建退出的菜单项
    QMenuBar *bar = new QMenuBar();
    setMenuBar(bar);
    QMenu *startMenu = bar->addMenu("开始");
    QAction *quitAction = startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    //返回按钮
    myPushButton *backbtn = new myPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backbtn->setParent(this);
    backbtn->move(this->width() - backbtn->width(),this->height() - backbtn->height());
    connect(backbtn,&QPushButton::clicked,this,[=]()
    {
        QTimer::singleShot(400,this,[=]()
        {
            //发送信号给主场景
            emit this->PlaySceneBack();

        });

    });

    //绘制胜利图片
    //表情包
    QLabel *win = new QLabel;
    QPixmap win_map;
    win_map.load(":/res/win.png");
    QSize picSize(win_map.width()*0.4,win_map.height()*0.4);//图片预计大小
    win_map = win_map.scaled(picSize, Qt::KeepAspectRatio);//等比缩放图片
    win->setGeometry(0,0,win_map.width(),win_map.height());
    win->setPixmap(win_map);
    win->setParent(this);
    win->move(this->width()*0.5-win_map.width()*0.5,win_map.height()*0.1-400);
    //文字图片
    QLabel *win_1 = new QLabel;
    QPixmap win_map_1;
    win_map_1.load(":/res/LevelCompletedDialogBg.png");
    win_1->setGeometry(0,0,win_map_1.width(),win_map_1.height());
    win_1->setPixmap(win_map_1);
    win_1->setParent(this);
    win_1->move((this->width()-win_map_1.width())*0.5,-win_map_1.height());


    //关卡信息（当前关卡数）
    QLabel *label = new QLabel;
    label->setParent(this);
    //颜色设置
    QPalette pe;
    pe.setColor(QPalette::WindowText,Qt::white);
    //字体设置
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString infor_str = QString("关卡 %1").arg(this->levelIndex);
    label->setText(infor_str);
    label->setFont(font);
    label->setPalette(pe);
    //    label->setFixedSize(60,20);
    //    label->move(0,500);
    label->setGeometry(QRect(15,this->height()-50,120,50));

    dataConfig data;
    //初始化关卡
    for(int i = 0;i<4;i++)
    {
        for(int j = 0;j<4;j++)
        {
            this->game[i][j]=data.mData[this->levelIndex][i][j];

        }
    }

    //金币背景的设置
    for(int i = 0;i<4;i++)
    {
        for(int j = 0;j<4;j++)
        {
            //绘制背景图
            QLabel *coin_bg = new QLabel;
            coin_bg->setGeometry(0,0,50,50);
            coin_bg->setPixmap(QPixmap(":/res/BoardNode(1).png"));
            coin_bg->setParent(this);
            coin_bg->move(57+i*50,210+j*50);

            //创建金币
            QString coin_cate;
            if(this->game[i][j]==1)
            {
                coin_cate=":/res/Coin0001.png";
            }
            else
            {
                coin_cate=":/res/Coin0008.png";
            }
            MyCoin *coin = new MyCoin(coin_cate);
            coin_box[i][j]= coin;//金币入库
            coin->setParent(this);
            coin->move(59+i*50,214+j*50);

            //给金币属性赋值
            coin->postion_x=i;
            coin->postion_y=j;
            coin->direction=this->game[i][j];

            //点击后翻转
            connect(coin,&MyCoin::clicked,this,[=](){
                if(!coin->is_animation)
                {
                    //一个翻转动画的播放时，禁止鼠标点击事件
                    for(int i=0;i<4;i++)
                    {
                        for(int j =0;j<4;j++)
                        {
                            coin_box[i][j]->result_coin=true;
                        }
                    }
                    coin->change();
                    this->game[i][j]=this->game[i][j] == 0 ? 1 : 0;//数据改变



                    //延时翻转  (bug是快速点击时，周围按钮会翻转多次，但自身不转)
                    //bug已修复，是动画未判断
                    QTimer::singleShot(200,this,[=](){
                        //翻转周围
                        //上
                        if(coin->postion_x-1>=0)
                        {
                            coin_box[i-1][j]->change();
                            this->game[i-1][j]=this->game[i-1][j] == 0 ? 1 : 0;//数据改变
                        }
                        //下
                        if(coin->postion_x+1<=3)
                        {
                            coin_box[i+1][j]->change();
                            this->game[i+1][j]=this->game[i+1][j] == 0 ? 1 : 0;//数据改变
                        }
                        //左
                        if(coin->postion_y-1>=0)
                        {
                            coin_box[i][j-1]->change();
                            this->game[i][j-1]=this->game[i][j-1] == 0 ? 1 : 0;//数据改变
                        }
                        //右
                        if(coin->postion_y+1<=3)
                        {
                            coin_box[i][j+1]->change();
                            this->game[i][j+1]=this->game[i][j+1] == 0 ? 1 : 0;//数据改变
                        }
                        //所有动画结束后，启动鼠标
                        for(int i=0;i<4;i++)
                        {
                            for(int j =0;j<4;j++)
                            {
                                coin_box[i][j]->result_coin=false;
                            }
                        }



                        //判断是否胜利
                        this->result=true;
                        for(int i=0;i<4;i++)
                        {
                            for(int j=0;j<4;j++)
                            {
                                bool bre = false;
                                if(coin_box[i][j]->direction == false)
                                {
                                    this->result=false;
                                    bre=true;
                                    break;
                                }
                                if(bre){break;}
                            }
                        }
                        if(this->result==true)
                        {
                            qDebug()<<"胜利";
                            //禁用按钮
                            for(int i=0;i<4;i++)
                            {
                                for(int j=0;j<4;j++)
                                {
                                    coin_box[i][j]->result_coin=true;
                                }
                            }

                            //胜利图片的出现动画
                            //创建动画对象
                            QPropertyAnimation *pixup = new QPropertyAnimation (win,"geometry");
                            //设置动画时间间隔
                            pixup->setDuration(1000);
                            //起始位置
                            pixup->setStartValue(QRect(win->x(),win->y(),win->width(),win->height()));
                            //结束位置
                            pixup->setEndValue(QRect(win->x(),win->y()+380,win->width(),win->height()));
                            //设置曲线
                            pixup->setEasingCurve(QEasingCurve::OutBounce);
                            //执行动画
                            pixup->start();

                            //创建动画对象
                            QPropertyAnimation *pix_1up = new QPropertyAnimation (win_1,"geometry");
                            //设置动画时间间隔
                            pix_1up->setDuration(1000);
                            //起始位置
                            pix_1up->setStartValue(QRect(win_1->x(),win_1->y(),win_1->width(),win_1->height()));
                            //结束位置
                            pix_1up->setEndValue(QRect(win_1->x(),win_1->y()+514,win_1->width(),win_1->height()));
                            //设置曲线
                            pix_1up->setEasingCurve(QEasingCurve::OutBounce);
                            //执行动画
                            pix_1up->start();
                        }


                    });
                }




            });


        }
    }



}
void playScene::paintEvent(QPaintEvent *)
{
    //背景图片
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/bg3.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //画背景的图标
    pix.load(":/res/Title.png");
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);
}


